package com.yueyang.sojourn.server.controller.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/1/31
 */
@Data
@ApiModel(description= "返回响应json数据")
public class JsonResult<T> {
    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码，具体按照相应接口文档约定处理。默认成功时返回0")
    private int code;
    /**
     * 消息
     */
    @ApiModelProperty(value = "响应消息")
    private String message;
    /**
     * 是否成功
     */
    @ApiModelProperty(value = "表示请求是否成功")
    private Boolean success;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public JsonResult<T> of(T data) {
        this.data = data;
        return this;
    }

    public JsonResult(int code, String message, Boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }


    public static JsonResult ok() {
        return ok("ok");
    }

    public static  JsonResult ok(String message) {
        return ok(0, message);
    }

    public static  JsonResult ok(int code, String message) {
        return new JsonResult(code, message, true, null);
    }

    public static  JsonResult fail(int code, String message) {
        return new JsonResult(code, message, false, null);
    }

    public static  JsonResult fail(String message) {
        return fail(-1, message);
    }
}
